﻿Public Class frmLeaveTypeBR
    Private frmParent As frmMain
    Dim validator As PropertyValidator = New PropertyValidator
    'Dim sqlManager As New SQLManager
    'Dim sqlList As New SQLList
    Dim payrollManager As New PayrollManager
    Private Sub frmLeaveTypeBR_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = frmMain
        ClearAllFields()

        Try
            'Dim leavetypeList As List(Of LeaveType) = sqlList.getAllLeaveType()
            Dim leavetypeList As List(Of LeaveType) = payrollManager.getAllLeaveType()
            For Each leavetype As LeaveType In leavetypeList
                Dim item As ListViewItem
                item = lvwLeaveType.Items.Add(leavetype.type)
                item.SubItems.Add(leavetype.description)
                item.SubItems.Add(leavetype.id)
            Next
            grpLeaveType.Text = "Total Record(s): " + lvwLeaveType.Items.Count.ToString()
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        frmLeaveTypeDE.ShowDialog()
        frmLeaveTypeBR_Load(vbNull, EventArgs.Empty)
    End Sub
    Public Function ClearAllFields()
        validator.ResetControls(grpLeaveType)
        btnAdd.Enabled = True
        btnEdit.Enabled = False
        btnDelete.Enabled = False
        grpLeaveType.Text = "Total Record(s): 0"
        validator.resizes(lvwLeaveType)
        Clipboard.Clear()
        Return 0
    End Function

    Private Sub lvwLeaveType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwLeaveType.SelectedIndexChanged
        btnEdit.Enabled = (lvwLeaveType.SelectedItems.Count = 1)
        btnDelete.Enabled = (lvwLeaveType.SelectedItems.Count > 0)
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Clipboard.SetText(lvwLeaveType.SelectedItems(0).SubItems(2).Text)
        frmLeaveTypeDE.ShowDialog()
        frmLeaveTypeBR_Load(vbNull, EventArgs.Empty)
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Try
            For i As Integer = 1 To lvwLeaveType.SelectedItems.Count
                'sqlManager.executeQuery("DELETE FROM ref_leave_type WHERE id = " + lvwLeaveType.SelectedItems(i - 1).SubItems(2).Text)
                payrollManager.deleteItemLeaveType(lvwLeaveType.SelectedItems(i - 1).SubItems(2).Text)
            Next
            MsgBox("Record(s) has been deleted.")
            frmLeaveTypeBR_Load(vbNull, EventArgs.Empty)
            grpLeaveType.Text = "Total Record(s): " + lvwLeaveType.Items.Count.ToString()
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
    End Sub
End Class